#include <stdio.h>

#include <unistd.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <signal.h>
#include <string.h>

#include <stdlib.h>



typedef struct _src_file_info
{
    char file_name[128];
    struct stat stat_info;
    FILE * fp;
}src_file_info;

src_file_info pps_src;
int flag_exit_pps = 0;

#define LEN_ONE_PPS 12

int main(int argc, char * argv[])
{
    //char file_name[128] = "20220115064913";
    struct stat file_stat_temp;
    //getFileName(file_name);
    memset(&pps_src.stat_info,0,sizeof(struct stat));
    pps_src.fp = NULL;
    sprintf(pps_src.file_name,"%s","pps_rise_time.txt");
    flag_exit_pps = 0;
    unsigned char buff[1024];
    
    while(!flag_exit_pps)
    {
          if(pps_src.fp == NULL)
             {
                 pps_src.fp = fopen(pps_src.file_name,"rb"); 
             }
             if(pps_src.fp != NULL)
             {
                 stat(pps_src.file_name,&file_stat_temp);
                 //printf("size:%d\r\n",file_stat_temp.st_size);
                 if(file_stat_temp.st_size > pps_src.stat_info.st_size )
                  {
                      int cnt_to_read = file_stat_temp.st_size - pps_src.stat_info.st_size;
                      int cnt_read_once = 0;
                      int cnt_read_ret = 0; 
                      while(cnt_to_read>LEN_ONE_PPS)
                      {
                         cnt_read_once = cnt_to_read > 1024?1024:cnt_to_read; 
                         memset(buff,0,1024);
                         cnt_read_ret = fread(buff,1,cnt_read_once,pps_src.fp);
                         if(cnt_read_ret > 0)
                         {
                             //fwrite(buff,1,cnt_read_ret,fp_write);
                             cnt_to_read -= cnt_read_ret;
                         }
                         else
                         {
                            break;
                         }
                         
                      }
                      memcpy(&pps_src.stat_info,&file_stat_temp,sizeof(struct stat));
                      //printf("buff:%s\r\n",buff);
                      char *p_start = strrchr((char *)buff,(int)'p');
                      if(p_start==NULL)
                      {
                          continue;
                      }
                      char * p_dot = strchr((char *)p_start,(int)'.');

                      if(p_dot)
                      {
                          char str_num[16] = {0};
                          int len_int =  p_dot-p_start-1;
                          if(len_int<0 || len_int>16)
                          {
                              continue;
                          }
                          //printf("len_int:%d\r\n",len_int);
                          memcpy(str_num,p_start+1,len_int);
                          //printf("str_int = %s\r\n",str_num);
                          int sec = atoi(str_num);

                          int len_f =  strlen(p_dot) - 1;
                          //printf("len_f:%d\r\n",len_f);
                          memset(str_num,0,16);
                          if(len_f < 0 || len_f > 16)
                          {
                              continue;
                          }
                          memcpy(str_num,p_dot+1,len_f);
                          //printf("str_point = %s\r\n",str_num);
                          int msec = atoi(str_num);

                          printf("sec:%d,msec:%d\r\n",sec,msec);
                          
                      }
                  }

             }
    }
}
